package com.bsoft.springboot.lab47.server.autoconfigure;

import com.sun.net.httpserver.HttpServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
 * @author: hdh
 * @Desc:  配置类  自动装配
 * @create: 2024-02-27 10:53
 **/
@Configuration   //配置类
@EnableConfigurationProperties(BsoftServerProperties.class)   //使配置文件生效 引入

public class BsoftServerAutoConfiguration {
    private Logger logger = LoggerFactory.getLogger(BsoftServerProperties.class);

    @Bean//声明创建
    @ConditionalOnClass(HttpServer.class) //肯定存在的类
    public HttpServer httpServer(BsoftServerProperties bsoftServerProperties) throws IOException{
        //创建httpServer对象并启动
        HttpServer server = HttpServer.create(new InetSocketAddress(bsoftServerProperties.getPort()),0);
        server.start();
        logger.info("[httpServer][启动服务器成功，端口为:{}]", bsoftServerProperties.getPort());
        return  server;
    }
}
